package com.lsy.poi.cell.validator;

import java.util.Date;

import org.apache.poi.xssf.usermodel.XSSFCell;

import com.lsy.poi.exception.ParseException;
import com.lsy.poi.exception.cell.UnEquivalenceException;

public class EquivalenceValidator extends NotBlankValidator {
	
	private Object expected;
	

	public EquivalenceValidator(Object expected) {
		super();
		this.expected = expected;
	}

	@Override
	public void doValidate(Object cellValue, XSSFCell cell) throws ParseException {
		
		super.doValidate(cellValue, cell);//先使用  NotEmptyValidator（父类） 进行 验证
		
		boolean result;
		
		if(expected instanceof String) {
			result = 
					( (String)expected ).equals( (String)cellValue );
				
		} else if(expected instanceof Date) {
			result = 
					(  ((Date)expected).getTime() == ((Date)cellValue).getTime()  );
		} else {
			result = 
					(expected == cellValue);
		}
		
		if(!result)
			throw new UnEquivalenceException(cell, expected);
		
	}
	
	public static void main(String[] args) {
		Exception e = new Exception("与 预期值 不相等");
		System.out.println(e.getMessage());
	}

	public static void main1(String[] args) {
		/** 
		 * 日期比较
		 * 
		Date d1 = new Date(123456);
		System.out.println(d1);
		
		Date d2 = new Date(123456);
		System.out.println(d2);
		
		System.out.println(d1.hashCode());
		System.out.println(d2.hashCode());
		
		System.out.println(d1 == d2);
		System.out.println(d1.getTime() == d2.getTime());
		*/
		
		double d1 = new Double(12.5);
		double d2 = 12.50;
		System.out.println(d1 == d2);
		
		
	}
}
